package org.example;

import sun.security.mscapi.CKeyPairGenerator;

import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;

/**
 * Hello world!
 *
 */
public class App
{
    static String orgPubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDudETjwVGPbfCSMkb4n47VNFBfy5kmuQZxWhDSjudaFTw7LvmEiPTONyG/wNsYuzD95gpIeAA+WqWdxAQ3O5NcBap7mbKSHkzSpt/nWXkwdNC3CE7uXl8CZRuzkTm5GTKWnjhbjndSQzeSHA5PqK2Kv/KRGm0dENRrSWnbKhol3QIDAQAB";
    static String orgPrivateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAO50ROPBUY9t8JIyRvifjtU0UF/LmSa5BnFaENKO51oVPDsu+YSI9M43Ib/A2xi7MP3mCkh4AD5apZ3EBDc7k1wFqnuZspIeTNKm3+dZeTB00LcITu5eXwJlG7ORObkZMpaeOFuOd1JDN5IcDk+orYq/8pEabR0Q1GtJadsqGiXdAgMBAAECgYEAqPwkxfL+RdL3peOTgCYzJszIGNtEUbjV5Aab3VwAgEqEQLztfws/0dQoSqf1nPUrpwMbTngcVBQRQljjU/EYX8QO6Vl404un4fbGZ0sTGCXnWVnZVfU+ErVBsQxQzVPo4faOYRPPLEVq4nXLsBDlhvCC0Akt5oGU1wNNP8NbbwECQQD/ta5Ek+oVdXlCeXIryquGwXPJG9fpsH0JcCXvEJ9s9vXRSvmiv9wL35huo+ZHdHSEg5PcMWDsl7VEt2Ht7UVxAkEA7rmSv6qAaVlE+6uAN2RJqMPtt0Irbmt+v1M88tL+We/4jgtZC55qLLic1EvuU4tfAGa325+uNcTav+fhDleBLQJAdd1IdlO3kiyTuBBCj+zbI0IIHKq42v5+o9sNoSQIWDF9HPtyFZJZ2x81I8poWodTLcF4ZgZcprFG3GWqvgCgAQJBAM0uIrspyITDfU7x8JmrUMQE4mQIM1wMeDvpRxFN199lKbSsfDRIS+l0Ixc0y/pyZ4zovE2/yPPd5kXa/sxc2WECQGpJLlvrXhkaDN+5z7aeWOMyr7wj3sauwrkV/bAXUe9yFpvtWiW2nYO3K26oTBhKzhKc1rrZtKBYXnBiePkZmz4=";

    // 线上环境
    static String orgPubKey1 =
        "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3mx6Ov42skK0V+zbqQSObSZ6+\n" +
        "HhCa74AVXJdW1yotUTYi1GhRJqn93gA5tuqOyEHQkCFg8knVk0qx1m5/UxDBglfX\n" +
        "6BDjtCw2rEwX/iPi3YBxAmvGPk9k3vHtGF6zyIZupzTu0v9dY3mo71pBY56je9/h\n" +
        "juUBNKfZucgmGkWR0QIDAQAB";

    static String orgPrivateKey1 =
        "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALebHo6/jayQrRX7\n" +
        "NupBI5tJnr4eEJrvgBVcl1bXKi1RNiLUaFEmqf3eADm26o7IQdCQIWDySdWTSrHW\n" +
        "bn9TEMGCV9foEOO0LDasTBf+I+LdgHECa8Y+T2Te8e0YXrPIhm6nNO7S/11jeajv\n" +
        "WkFjnqN73+GO5QE0p9m5yCYaRZHRAgMBAAECgYAiUbJiiFeY++n+g9h97PMU4av0\n" +
        "aXY9H6tXUtlO+CUAF4AA/qcJFCsOMuLj5tbtHW1/6JD0qBU5ALEjkKtSzVoCIHCt\n" +
        "1quoliBKj5fxlWUIz0mq2Ndb6bXsc1smE0hY9+PCWoUgCtohcvBBYF5+TGk5iZoJ\n" +
        "pTjcgFBeXsUHyBTcYQJBAOlHQWSDQdmMuW7eWfnh+hW9sV2gUre/PrMCGXKndzys\n" +
        "re+nOph8vBzhgYKWqPo14B66t6w2YoF2NivKkWkpHnUCQQDJfUsV+ZCYxq3vQoAs\n" +
        "KN6ImN3EmNzJhgf33hNLIprf7cSZIxIk+1SO0umYfDxbtctJrVm3KXbCo+UGasgz\n" +
        "9fJtAkBvdVVAQhqnaD59Q433/8mus5jYJC8BjkXV383q4yrseZENrXgovYhfwDtR\n" +
        "+sw36J/x7sKtbLq6IpIVcE7z0Oq1AkBXK2aNpj+haOsdBNcww6mqzP+8qylx4hOW\n" +
        "b7RHIDyCvnn8ioiyCS3G8IZxGuZ+AsNlJ6MVs9GWWZOe2BmvKSttAkEAzt12jTOm\n" +
        "Chue3HTU+FCb61lY6bhrrRUQ6f05VihNZlWfelxi31qq21vbs6kf7snteh9IIzJE\n" +
        "MthmMTwxt0ruzQ==";

    static String sipPubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCHPTxKy8y9MOGI2h52DuVvk0RlOv2NjBTY88frHLk3qRYU24ymdGV4H++ta8DZiMJNa1OI1oAyYMVlC8OZaENWTnUPJ2Et76jnQ1zdtnm1fl75l863nYyJx+qJwohBTqIHGtQpLp6pZHLWLYg6B7UYkpna7TOTjEpKmVop6qO2cQIDAQAB";
    static String sipPrivateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIc9PErLzL0w4YjaHnYO5W+TRGU6/Y2MFNjzx+scuTepFhTbjKZ0ZXgf761rwNmIwk1rU4jWgDJgxWULw5loQ1ZOdQ8nYS3vqOdDXN22ebV+XvmXzredjInH6onCiEFOogca1CkunqlkctYtiDoHtRiSmdrtM5OMSkqZWinqo7ZxAgMBAAECgYAJS1QRb+AqQGm/lf3x7yQlfuF8c4lpaO8l2dsrdVXlVWGHHW+VIPq4PrmiGX2vdY6k83NSPCujSrNGNoDC0j+OXM6hCmE3FBzws7K0Bt7PnS3f/1l2Rsx8aGDq+XpJEhUICx/v5GfXeqmTgm/0HmgEcZO/QfRI3LpRkEsMkx8OgQJBAONjWcgvyP2uWLmvZlfawWgLbusG0buxXH+AOFHwDg3TtWPi7boRS6IRzOFO/iya6+UZc3UVQMkdzcC8SHjZRGUCQQCYQZM2JB/7TJXG3pccEokBGQuDxjbOJZEKuifs4qCW2nm4Trr7KRwjoFgwSiPZgMa7li7C8ZoYBe9IqspbwisdAkBG6XbYXT6bDkIRNkf+YfQq1FX06Z2CoR8ti/kEZI6ddUZ+LgjhG/+wPUjdgtr1YiLqwXMmMkiP78F4t78KIQeZAkEAjXvCNHl1TqxoMhj+CpQew+pmDNnQa9f05CcAmtwtpoD2wxJGJsaY43JJAPakQaWtBvwUEAs6ykAZj0lGKeZQ8QJABb7ocsVGdLlPCe/2oKdUuWCDS4U72DmLP1xz20nFzfYAuuch4WTz+8cYW8Gb0HvWLBkij8ov3aLiYXZnddBFPA==";

    static String app_key = "3WkOezCc45P0";
    static String app_secret = "2Nr7GKcHLDUZm3ntz6OW32d50vyl8u5T";

    public static void main( String[] args )
    {
        try {
            //原始数据
            // String origData = "{\"cityId\":\"72\",\"mobileNo\":\"13262531528\",\"productNo\":\"HMT_QJF_A\",\"sipOrderNo\":\"202111261827V0115045\",\"timestamp\":\"1637922454\",\"userId\":\"24075503\"}";
            String origData = "hello";

            //供应商公私钥
            RSAPublicKey orgPubKeyObj = RSAHelper.getPemRSAPublicKey(orgPubKey);
            RSAPrivateKey orgPriKeyObj = RSAHelper.getPemRSAPrivateKey(orgPrivateKey);

            //sip公私钥
            RSAPublicKey sipPubKeyObj = RSAHelper.getPemRSAPublicKey(sipPubKey);
            RSAPrivateKey sipPriKeyObj = RSAHelper.getPemRSAPrivateKey(sipPrivateKey);

            String sign = RSAHelper.sign(origData, orgPriKeyObj);
            sign = "I3WExicmEt1r407Gw3xyXZEP5eob4eY1QTP1nLQ+VMZNeIDsG1utZIzhNqGFwvoFJ527VK00STujP7z/XOr6MlNJRfeY0nQDQiEh4DAyG+derRkI8cgp8dc9hce/mfnubNUvAwN0Tz9yNUn7ROTf5C5rfn98gUGZL7EX6TF3Two=";
            System.out.println(sign);
            Boolean res = RSAHelper.verify(origData, orgPubKeyObj, sign);
            System.out.println(res);
            System.exit(0);

            // System.out.println(RSAHelper.verify(data1, orgPubKeyObj, sign));
            // System.out.println(RSAHelper.verify(data1, orgPubKeyObj, RSAHelper.sign(data1, orgPriKeyObj)));
            // System.out.println(RSAHelper.sign(data1, orgPriKeyObj));
            // System.exit(0);
            //sip公钥对数据加密，供应商私钥对数据加签
            // TZ加解密
            // String tzEncData = RSAHelper.encrypt(orgPubKeyObj, origData);
            String tzEncData = "JmNaI6xTDpRspE67BeumVrPnhFM/VcvcDaX3O1fX2Vl01T65/hRI/GMur8WNFUbdBRgA477ACnUJLJhHQDV+MpqhGFm5qHd+UVyNE71Q51z7q+Qvrjx6e0WdNg5ishHvKCKEbHXQa0MnKOLRPGtNDtP6VIAd1/03kc4dA4v4i39GSeWs7lTICt16xYf7SUMz2xiW6dPfatsry2RiMP7HawP9pJKgVbkZyQe8G2IwrnLHyRUatF65oENgbnnfx3cjsbOjiRlex73wRpdIQ/rSDIZsGzGGItKAQtfuupitwRp2JGkspaB35YEDdn0aMzvAzFi+O6bouHd6UgkkoaB2Kw==";
            // String tzEncData = "qlWw5gCz w wWWGUGgvt8UJcN\\/H0m\\/rvxKac9oYUFEye4dscNNLkFkTSMfAyQbCzCE8ZPHl4iy6dYNoAvB\\/lpDLkA0n\\/wf0x9iVRF wwydoTHZO0FV9pTB1659r5pcTQM5qgVO5hffkw4FE8WmodLbUcy0jk2P99tZqDzu2JnIWkFqrAoFw1QVdShgR gzV7wcLw7xe7wHKvB5xIRjHA\\/EpZ qmYfIEYUAjyIAsCPONPv6EOb1udHBxPgTZBO1yiIKPfkZrcs4IFomc9hKwzRMd3hGL VPz2GZRKpwCGjdEIbFMd9ssrzViPPTYxwENQcxzzJsK1ialVDejzPAhr5w==";
            // String tzEncData = "PFEBJ1HE7kKpex4FhSHx7e05rR0bm9gqwVKZOLOuoHQ0cWPxFYU5D2PClSUw6hzGPb6P8S9URbEAhtwiM8SKcp5sAiVfxBYkWuP/czC4VHgSVHZBi+c2JVCpTqt+CEro761w7yb+QjiK3IPdCk0jh4SORHwAb1HpF3sFVZwxkWCCdLQMrKsa3SyA3BhROpBaNFl9H6AFoQy0eMHKgpge5S3+Cg1ylDDdcoYF1CjnlyYt1xjlCuqk0rAs0SLL16W41aXb3k0yixfAssbPK5UgpQFu+RyXsQYvDEogTvYLhTTW5Nte4INPKjktj/CDH057FfHWlniiAp2b1aMQj5qb5w==";
            tzEncData = RSAHelper.fixRsaContent(tzEncData);
            String tzDecData = RSAHelper.decrypt(orgPriKeyObj, tzEncData);

            // System.out.println(tzEncData);
            System.out.println(tzDecData);
            System.exit(0);

            String data = RSAHelper.encrypt(sipPriKeyObj,origData);
            String signData = RSAHelper.sign(origData,orgPriKeyObj);
            System.out.println("data = " + data);
            System.out.println("signData = " + signData);
            //sip私钥解密，供应商公钥验签
            data = "PdlKPzEXrwTVL39kFh3AGOatB7Zf7/ORnjOIsdmygssDfRu1jPAXAL5/211VtJP3EVaPIUw7q7KSG/HetL52R3Ip1XpDHaalbxq1r3as1or4BtwBtQQcYS7huKgpyO3wGjGM8pWY+KZDNlZRktXr90/m97dnkg0e/7vZ5wApGQwk47dzFR6HahqBeGbGm6Sdw9VkwSBRl0xBpujFmIZD1ljNc7shT5leW0iOZiW" +
                "Pcn4pnDmrcsBNE9xW+K4RFDPRw+NcBIKb8FFWBa4YVOP++UkYSKUrMpnJQXH8KA+2Ai9s9SSq/SoHNIJxanLKLqASq7unpqWKnqinQdmpzdQTugXStW8GAKMfDjdKOoEVg6A39pDQnnBGEqqPkn7g1PnHnnBCZh72QDVQ13QwIw6DxjrGfQJZgE4STPbil6Y/kDqj/BTswDGT4TZynOGdoES6J4LGI5iZwf+/iNq" +
                "qbDFPRUC/+RkCXzONjxbEQYtcML+edsqLtOUbMh2G29wLNFO5fDW5EtNEGS18j0TuJPXyL1ZrOK0z1MWbAAkIQp22eQCTfIJ9fe6MdYBRWQy1PhdNWqUJ2MMHISOcM7k3dzv5wUp2zDLBuZGpwubVhYhh+4QkBGIzQrZ/+q19Moxd1sEAuC6BdEHGBfU1SUzVicqEE1DSRrkD9gk3YIb2RvwiZU4HddylK8oyvkF" +
                "TG7izNOLNMq7Hi8xTbz8Ujdfr68608i2GIA2CKYflAlmGODIHjNDrVGD7/fWe3u5qsIztg1RuYl3/KmQwaiY7oT6rXWd/1r3dxM7A90AsH5bpf/gZz+zKxnC0zkjXYtjigrmV1AltVZVnGCwU2uVd7SsmHvMCeFccjtrHdairsjKfCpsGLFaTJ8tlF1/1OXHGzy10EDurpod/OLlYSfdhzuAOlt+PMB8BJ4dVIRY" +
                "g/RMdlEzszlbb7vhHLKHh4GlQ8dmolRzzLKNExGAdNpg5EvYtQadM/VvuUJ2M0UF2kx/1xejpATNPlT6nm2NiJeXNYex13sQhYEXtvj9Bo/y3ntZHZ34lnrDBwrEeYMK6CR0wRgGMQ1txBMMSTNc4gGMKCJTuqmmWZA1RPjtER5ewS4rqB4cHefi+Wx3HazThsSdUpXhW22yaqwi7wZMfeG1TeLLVc51HLe8vFTH" +
                "2CeP2PgPkxwOqt4Sbmlc4Lw19i7qWBnsVIB6GbLS0mRX0w6DADyPWSqLL02gCIG7EEpaqRcdp5n9Wo/dxAQLcLm/vwyt6pHgRafNdfJ4+oSQIYDfvdkwuPZYBfRUioNrLH3uvfdMct+HJnYRN9cN1OBlgXOCcPNNjXjF+7fPAIIhnWsbn3PA7plSewdxSokMzcnJCHRzqdOwCcUFpNu3fzGXvyM5Df8ihFSS31rK" +
                "jVGZgJqGDDLe55/HaVjRAK4YRgxu9twK3F0vN2/YGoXO55FxPaUk0qKnFT4I27jloaY29dX36q/B1B3lhcgKJOTf1Qo8ROh1DDcmcjzehvnAoTQXBrQoxoHM1bL/9O5oQaBz0EKT9NjXbIhsFKZOcEO21BeZeirFnaHcUOxEFpk/u1Epg7MIBm0zg3RvupvqKY5B0Nt5PyHOGPetn/m3bzm6k1cq7neGWmqBkjQl" +
                "tV3YYqwacJUJC0wab1jQY+tNwui8etI+o2DM/bRNd1ygz16e5hRmmJuwb1w5KEgx8t0Awn1IbQjO9a3z3QYVuM0pyuVrj80fuSxSJZsGlOyAWq6pOxnRpaOrrViPboEJVi5o9xDe5KGjj8MqSdYVrWZrAGdJwV//cEXP8l1BDmXseZaRpQdNMYa6RoqkhzlCw3IJ9O3hHN2jzbJLunlk6+Wqs7kBkxm3YfnFTlZ/" +
                "nepHJ5u/ygbkJCxLaFp9PADUr284iGZrJ4friQIXIVvLhXeAp6faZBqrnLasrwtMO9LEfcxyiU15/YZFhre/24mQoBnHz6BvPWbgWCvnRYZtCJNIyUy/CMOcP9QqBhbdohdHdQVdQVGzdFDGBW+1jyQuV5XwSiKs8nbkjdBjLNFFZNlkWH9EKMR7F4TmWy7SM";
            // String decData = RSAHelper.decrypt(sipPubKeyObj,data);
            // data = "YuaP4p4xbBGS5dz3/6sznob7sBttNAGAfdVPanF7GIGzymLvmd/u6pDEZC0qXelAe56xX0mpI38kUqy96oiK8jqgHlyEcuMZh0gGPZHH66z7iFFdeT3+DyBuRzWUyZWuUWFqG8KcpD0pv6SEMwidBJaQvYt48BMKdGxk0aq5y7w=";
            String decData = RSAHelper.decrypt(sipPriKeyObj,data);

            System.out.println(decData);
            System.out.println("verify = " + RSAHelper.verify(decData,orgPubKeyObj,signData));}
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
